//
// Created by jyh on 2021/7/30.
//

#ifndef BBOVRSCHIP_DEPLOY_H
#define BBOVRSCHIP_DEPLOY_H

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <malloc.h>
#include <time.h>
#include "Const.h"
#include "Antatx.h"
#include "cJSON.h"
#include "rtklib_fun.h"

// Typedef
typedef struct VrsRequest{
    char staname[LEN_SITENAME];
    int staid, msgid;
    double blh[3],x[3],dis;
};
typedef struct map {
    char key[257];
    double val;
};
typedef struct mTable{
    char antnam[LEN_STRING],antpcv[LEN_STRING],corrDCB[LEN_STRING],corrp1c1[LEN_STRING],database[LEN_STRING],gptgrd[LEN_STRING],gptgrd_w[LEN_STRING],\
         jpleph[LEN_STRING],leapsc[LEN_STRING],object[LEN_STRING],ocload[LEN_STRING],pndfln[LEN_STRING],polut1[LEN_STRING],satnav[LEN_STRING],stacoo[LEN_STRING],\
         ut1t03[LEN_STRING],ut1t10[LEN_STRING],ut1t96[LEN_STRING],baselines[LEN_STRING],subnet[LEN_STRING],rtconfigs[LEN_STRING],corrStaDCB[LEN_STRING];
};
typedef struct Satellite {
    struct Antatx AX;
    char cprn[LEN_PRN],type[LEN_SATTYPE],pcv[LEN_PCVTYPE],clk[LEN_CLKTYPE],sid[LEN_COSPARID],svn[LEN_PRN];
    double dclk0,qclk,sse_angle; // sun-spacecraft-earth angle here in rad
    double offs[3],xyz[MAXFREQ][3];
    double lamda[MAXFREQ];
    int ifreq,isabled;
    double freq[MAXFREQ];
    double g,g2,lamdw,lamdn,fac[2];
    double dx0[MAXICS],mass;
    struct map mlam[MAXFREQ], mlamw[MAXFREQ];
};
typedef struct RnxState {
    double obs[MAXSAT][2 * MAXFREQ], omc[MAXSAT][2 * MAXFREQ], sod;
    double vomc[MAXSAT][2 * MAXFREQ], dop[MAXSAT][2 * MAXFREQ],
            snr[MAXSAT][MAXFREQ];  //
    char fob[MAXSAT][2 * MAXFREQ][LEN_OBSTYPE];
    char staname[LEN_SITENAME];
    int mjd, flag[MAXSAT][MAXFREQ]; /* amb flag for reset ambiguity,slip flag
                                       for compute ewl/mw */
    double azim[MAXSAT], elev[MAXSAT], delay[MAXSAT];
    double var[MAXSAT][2 * MAXFREQ], amat[MAXSAT][MAXCOEFPAR][2 * MAXFREQ][2];
    double zmap[MAXSAT], hmap[MAXSAT];
    int rngOmcTag[MAXSAT], nCoefPar[MAXSAT][2 * MAXFREQ];
    double trpModel[MAXSAT], ionmodel[MAXSAT];
    double rleng[MAXSAT];
    double spndel[MAXSAT][MAXFREQ][2],
            lifamb[MAXSAT][MAXFREQ][2];  // delete time and living time
    double ioncor[MAXSAT], ionpart[MAXSAT];
};
typedef struct Station {
        struct Antatx AX;
        struct RnxState ob;
        char name[LEN_SITENAME], skd[LEN_SITENAME],clk[LEN_SITENAME],pcv[LEN_SITENAME];
        char map[LEN_ZTDMAP],obsfile[1024];
        double x[3],dx0[3],qx[3],vx[3];
        double geod[3],rot_l2f[3][3],dloudx[MAXSAT][3],xsat[MAXSAT][MAXFREQ][6],scalar[MAXSAT][MAXFREQ];
        double enu[MAXSYS][MAXFREQ][3],enu0[3];
        char   *rectyp, *anttyp;
        double rclock[MAXSYS],dclk0[MAXSYS],qclk[MAXSYS];
        double sigr[MAXSYS],sigp[MAXSYS];
        double cutoff,prephi[MAXSAT],olc[6][11];
        double ztdcor,dztd0,qztd,p0,t0,dt0,hr0,undu;
        double zcor,zdd,zwd,dgrd0,qgrd,grd[2],gcor[2];
        double dion0,qion;
        double vdop[5],ipp[MAXSAT][2]; /*correction of the dcb*/
        int nfixnl,lupztd,refsat[MAXSYS],lupclk[MAXSYS],first[MAXSAT],iBlock,staID;
        double refx[3],gausx[2],centralMeridian;
        char logFile[1024];

        int jdlog,omcflag[MAXSAT],isabled; // for detect range
        double sodlog,intvlog;

        int iode[MAXSAT];
        double rleng[MAXSAT];
        double azim[MAXSAT];
        double elev[MAXSAT];
};
typedef struct Deploy {
    bool EphTcpOn, ObsTcpOn;
    bool lpost, lbdtime, lsync, litrs, lqclog, lmthread, ldebug, lvrssav, lrawsav;

    time_t lastAct;
    int mjd, mjd0, mjd1, lastmjd, orbclk_intv, pred_intv;
    int nsit, nprn, nsys, maxthd, nthd, bnc_delay, obsSync_delay, tracelevel;
    double sod, sod0, sod1, lastsod, dintv, seslen;
    char sitname[MAXSIT][LEN_SITENAME], c1p1cortype[256];

    char uobs[256], cobs[256], refpoint[256], brdcmd[256], atomposturl[256], lineposturl[256], orbcfg[256], orbmutexfile[256], fixcomb[256];
    char system[MAXSYS], ztdmod[256], ztdpri[256], grdmod[256], qcdir[256], obsdir[256], ephdir[256], outdir[256], tracedir[256], precisedir[256];
    char freq[MAXSYS][MAXFREQ][LEN_FREQ], freq_obs[MAXSYS][MAXFREQ][LEN_FREQ], freqcomb_clk[MAXSYS][MAXFREQ][LEN_FREQ], freqcomb_dcb[MAXSYS][MAXFREQ][LEN_FREQ];
    int nfreq[MAXSYS], nfreq_obs[MAXSYS], nfreqcomb_clk[MAXSYS], nfreqcomb_dcb[MAXSYS], ifpb_order;
    int promode, iref, igloref, nfq[MAXSYS]; /* i_ifcbfq is the index of the freq[MAXSYS][MAXFREQ][LEN_FREQ] */

    char jsonConfig[LEN_STRING], tablesConfig[LEN_STRING];
    struct mTable mTable;

    char ptr_outProtocol[LEN_STRING], ptr_inProtocol[LEN_STRING], ptr_obsConnector[LEN_STRING], ptr_ephConnector[LEN_STRING], ptr_vrsConnector[LEN_STRING], ptr_atomConnector[LEN_STRING], ptr_webConnector[LEN_STRING];
    int vrslogport;

    char *cprn[MAXSAT];
    struct Satellite *SAT;
    struct Station *SIT;
    struct VrsRequest *m_reqs;
    struct Rnxobs *mRnxobs;
    char mbaselines[MAXSIT][256+1];

    stream_t* m_unsyncConn;
    pthread_t ephtid, vrstid;
};

// Variable
struct Deploy DLY;

// Function
int Map_index(struct map* map, char* key, int size);
char* json_loader(char *path);
void m_readJsonItem(cJSON* root, const char* item);
//void m_readJsonFile(int argc, char* args[]);
void m_readJsonFile();
void m_parseTablesConfigures(const char* t_path);
void m_parseFrequency(char freq_in[MAXSYS][MAXFREQ][LEN_FREQ], int* nfreq_in, char* freq_seq);
void Station_initial(struct Station *itemSit);

#endif //BBOVRSCHIP_DEPLOY_H
